package cn.minimelon.api.controller.module;

import cn.hutool.core.io.IoUtil;
import cn.minimelon.api.constant.UserConstants;
import cn.minimelon.api.controller.common.BaseController;
import cn.minimelon.api.domain.common.AjaxResult;
import cn.minimelon.api.domain.module.vo.DataPropVO;
import cn.minimelon.api.domain.module.vo.UploadVO;
import cn.minimelon.api.service.module.IDataPropService;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.UploadedFile;
import org.noear.solon.validation.annotation.HttpPart;
import org.noear.solon.validation.annotation.NoRepeatSubmit;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * 元属性A配置
 *
 * @author hiro
 */
@Controller
@Mapping("/module/prop")
public class DataPropController extends BaseController {
    @Inject
    IDataPropService propService;

    /**
     * 获取主题配置列表
     */
    @Get
    @Mapping("/list")
    public AjaxResult list(DataPropVO dataProp) {
        List<DataPropVO> list = propService.selectList(dataProp);
        return AjaxResult.success(list);
    }

    /**
     * 获取主题配置列表
     */
    @Get
    @Mapping("/simple/list")
    public AjaxResult simpleList(DataPropVO dataProp) {
        List<DataPropVO> list = propService.selectList(dataProp);
        return AjaxResult.success(list);
    }

    /**
     * 获取单个
     */
    @Get
    @Mapping(value = "/{uid}")
    public AjaxResult getInfo(String uid) {
        return AjaxResult.success(propService.selectById(uid));
    }

    /**
     * 插入属性
     */
    @NoRepeatSubmit(value={HttpPart.body})
    @Post
    @Mapping
    public AjaxResult add(DataPropVO dataProp) {
        if (UserConstants.NOT_UNIQUE.equals(propService.checkCodeUnique(dataProp))) {
            return AjaxResult.error("新增属性'" + dataProp.getName() + "'失败，属性编码已存在");
        }
        dataProp.setCreateBy(getUsername());
        if (propService.insert(dataProp) > 0) {
            return AjaxResult.success(dataProp);
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 修改属性
     */
    @NoRepeatSubmit
    @Put
    @Mapping
    public AjaxResult update(DataPropVO dataProp) {
        if (UserConstants.NOT_UNIQUE.equals(propService.checkCodeUnique(dataProp))) {
            return AjaxResult.error("修改模型'" + dataProp.getName() + "'失败，属性编码已存在");
        }
        dataProp.setUpdateBy(getUsername());
        dataProp.setUpdateTime(new Date());
        return toAjax(propService.update(dataProp));
    }

    /**
     * 删除属性
     */
    @NoRepeatSubmit
    @Delete
    @Mapping("/{uid}")
    public AjaxResult remove(String uid) {
        propService.deleteById(uid);
        return success();
    }

    @NoRepeatSubmit
    @Put
    @Mapping("/switch/active")
    public AjaxResult switchActive(DataPropVO dataProp) {
        dataProp.setUpdateBy(getUsername());
        dataProp.setUpdateTime(new Date());
        return toAjax(propService.updateEditFlag(dataProp));
    }

    /**
     * 上传模板
     */
    @NoRepeatSubmit
    @Post
    @Mapping("/upload")
    public AjaxResult upload(UploadedFile uploadedFile, UploadVO uploadData) throws IOException {
        uploadData.setUserName(getUsername());
        if (!uploadedFile.isEmpty() && uploadedFile.getExtension().contains("json")) {
            InputStream content = uploadedFile.getContent();
            uploadData.setUploadJson(IoUtil.readUtf8(content));
            propService.uploadPDManFile(uploadData);
            return success();
        }
        return AjaxResult.error("上传失败：不支持的文件");
    }
}
